<?php
/**
 * @TODO Fill Header
 *
 * @author Xavier VILAIN
 * @package
 * @subpackage
 */
use Fox\Common\Customer;
use Fox\Html\Input;
use Fox\Common\Customer\Type;
use Doctrine\DBAL\Connection;
use Fox\Generator;
use Fox\MaterialManagement\Material\Hierarchy;
use Fox\Html\DropList;
use Fox\Html\Screen;
use Fox\MaterialManagement\Material;
use Fox\Html\TextArea;

$xview = Xview::instance();
$id = Xview::instance()->val("customerid");
$type = Xview::instance()->val("type");
$excluded = array();

$searchHelp = new Fox\Html\SearchHelp("Partners");
$searchHelp->setMultiple(new Fox\Html\Button('select',"ReloadFromSelectLines('#b_GRPartners','MC_gPartners','?mod=customer&act=partners','hide(\'sh:Partners\')','InputSerialize(\'eCustomer\')');"));
$searchHelp->switchToObjectMode();
$ilink = $xview->currentURL();

$searchHelp->getSelBlock()->add(new TextArea("excluded"))
->excluded()
->hidden();
$searchHelp->getSelBlock()->add(new Fox\Html\Input("customerid",false,$id))
->excluded()
->hidden();
// Load already selected Product
$blItems = Xview::instance()->component("customer", "partners");
$data = $blItems->item("gPartners")->get();
if(is_array($data))
{
	foreach($data as $line)
	{
		if(isset($line['partnerid']))
		$Ids[] = $line['partnerid'];
	}
	if(!empty($id))
	$Ids[] = $id;
	if(isset($Ids))
	{
		$excluded = new \Fox\Dao\SqlFragment("customerid NOT IN (?) ",array($Ids),array(Connection::PARAM_STR_ARRAY));
		$searchHelp->getSelBlock()->item("excluded")->default = addslashes(json_encode($Ids));
	}
}
else
{
	$values = Xview::instance()->val("excluded");
	if(!empty($values))
	{
		$Ids = json_decode(stripcslashes($values),true);
		$excluded = new \Fox\Dao\SqlFragment("customerid NOT IN (?) ",array($Ids),array(Connection::PARAM_STR_ARRAY));
	}
}
// Filter By Customer Type
$finder = new Type();
$types = $finder->find();
$searchHelp->getSelBlock()->item("customer_type")
->options($types,Generator::useAttribute("type"),Generator::useAttribute("label"));
$searchHelp->getSelBlock()->item("customer_type")->id("type");

if(isset($_POST['name1']) && 1 == 1)
{
	$fragments[] = $searchHelp->screen()->fragments();
	$finder = new Customer();
	if(isset($excluded))
	$fragments[] = $excluded;

	$res = $finder->find(Fox\Dao\SqlFragment::combine($fragments),array("name1" => false));
	$searchHelp->grid()->getFieldcat()->field("customer_type")->id("type");


	$searchHelp->grid()->setDatas($res);
	echo $searchHelp->grid()->Display();
}
else
{
	echo $searchHelp->display();
}
